<template>
  <div class="login-container">
    <div class="login-box">
      <h1>气象站管理系统</h1>
      <p>Weather Station Management System</p>
      
      <form @submit.prevent="handleLogin">
      <div class="form-group">
          <label for="username">用户名</label>
        <input 
            id="username"
            v-model="loginForm.username" 
          type="text" 
            placeholder="请输入用户名" 
          class="input-field"
            required
        />
      </div>
      
      <div class="form-group">
          <label for="password">密码</label>
        <input 
            id="password"
            v-model="loginForm.password" 
          type="password" 
            placeholder="请输入密码" 
          class="input-field"
            required
        />
      </div>
      
        <div class="form-actions">
      <button 
            type="submit" 
        :disabled="loading"
        class="login-btn"
      >
        {{ loading ? '登录中...' : '登录' }}
      </button>
        </div>
      </form>
      
      <div class="register-link">
        <p>还没有账号？<router-link to="/register">立即注册</router-link></p>
      </div>
      
      <div v-if="error" class="error-message">
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const loginForm = reactive({
  username: '',
  password: ''
})

const loading = computed(() => userStore.loading)
const error = ref('')

// 检查是否已经登录
onMounted(() => {
  if (userStore.isLoggedIn) {
    console.log('用户已登录，正在重定向到首页')
    router.push('/')
  }
  
  // 如果有重定向参数，显示提示
  if (route.query.redirect) {
    console.log('检测到重定向参数:', route.query.redirect)
  }
})

const handleLogin = async () => {
  if (!loginForm.username || !loginForm.password) {
    error.value = '请输入用户名和密码'
    return
  }
  
  error.value = ''
  console.log('尝试登录:', loginForm.username)
  
  try {
    const success = await userStore.loginAction(loginForm.username, loginForm.password)
    
    if (success) {
      console.log('登录成功，用户角色:', userStore.roleCode)
      
      // 如果是被禁用的用户，显示提示
      if (userStore.isBanedUser) {
        ElMessage.error('您的账号已被禁用，请联系管理员')
        userStore.logoutAction()
        return
      }
      
      // 简化导航逻辑
      ElMessage.success('登录成功')
      
      // 使用window.location直接跳转，避免路由问题
      setTimeout(() => {
        window.location.href = '/dashboard'
      }, 100)
    }
  } catch (err) {
    console.error('登录处理错误:', err)
    error.value = err.message || '登录失败，请重试'
  }
}
</script>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-box {
  background: white;
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  text-align: center;
}

h1 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

p {
  margin: 0 0 30px 0;
  font-size: 14px;
  color: #666;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.input-field {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  box-sizing: border-box;
}

.input-field:focus {
  outline: none;
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.form-actions {
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: #409eff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
}

.login-btn:hover {
  background: #337ecc;
}

.login-btn:disabled {
  background: #a0cfff;
  cursor: not-allowed;
}

.register-link {
  margin-top: 16px;
  font-size: 14px;
}

.register-link a {
  color: #409eff;
  text-decoration: none;
  font-weight: 500;
}

.register-link a:hover {
  text-decoration: underline;
}

.error-message {
  margin-top: 16px;
  padding: 10px;
  background-color: #fef0f0;
  color: #f56c6c;
  border-radius: 8px;
  font-size: 14px;
}
</style>